package com.efa.backend.framework.utility;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.StringUtils;

/**
 * 该类是对 的处理类
 * 
 * @author 郭 荣
 * @since 项目 1.0 版本
 * @history 2009-10-15 郭 荣 新建
 */
public class DateUtil extends Utility {
	/**
	 * 添加天
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date addDay(Date date, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, day);
		return cal.getTime();
	}

	public static Date addMonth(Date date, int month) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, month);
		return cal.getTime();
	}

	/**
	 * 添加年
	 * 
	 * @param date
	 * @param year
	 * @return
	 */
	public static Date addYear(Date date, int year) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, year);
		return cal.getTime();
	}

	public static boolean afterCurrDate(Date date) {
		return DateUtil.currDate().compareTo(date) < 0 ? true : false;
	}

	public static boolean afterOrEqualsCurrDate(Date date) {
		return DateUtil.afterCurrDate(date) || DateUtil.equalsCurrDate(date);
	}

	public static boolean beforeCurrDate(Date date) {
		return DateUtil.currDate().compareTo(date) > 0 ? true : false;
	}

	public static boolean beforeOrEqualsCurrDate(Date date) {
		return DateUtil.beforeCurrDate(date) || DateUtil.equalsCurrDate(date);
	}

	public static Calendar currCalendarDate() {
		return Calendar.getInstance();
	}

	public static Date currDate() {
		return Calendar.getInstance().getTime();
	}

	public static String currDate(String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(DateUtil.currDate());
	}

	/**
	 * 格式化日期(yyyy-mm-dd)
	 * 
	 * @param date
	 * @return
	 */
	public static String dateFormat(Date date) {
		if ((null == date) || date.toString().equals(StringUtils.EMPTY)) {
			return StringUtils.EMPTY;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		return format.format(date);
	}

	/**
	 * 方法说明 自定义日期格式
	 * 
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String dateFormat(Date date, String pattern) {
		if ((null == date) || date.toString().equals(StringUtils.EMPTY)) {
			return StringUtils.EMPTY;
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 14位格式化日期
	 * 
	 * @return String
	 */
	public static String dateFormat14(Date date) {
		if ((null == date) || date.toString().equals(StringUtils.EMPTY)) {
			return StringUtils.EMPTY;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(date);
	}

	/**
	 * 8位格式化日期(标准日期)
	 * 
	 * @return String
	 */
	public static String dateFormat8(Date date) {
		if ((null == date) || date.toString().equals(StringUtils.EMPTY)) {
			return StringUtils.leftPad(StringUtils.EMPTY, 8, "0");
		}
		SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy");
		return format.format(date);
	}

	public static String dateFormat8yyyyMMdd(Date date) {
		if ((null == date) || date.toString().equals(StringUtils.EMPTY)) {
			return StringUtils.leftPad(StringUtils.EMPTY, 8, "0");
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		return format.format(date);
	}

	/**
	 * 8位格式化日期
	 */
	public static Date dateParse(String date) {
		try {
			return new SimpleDateFormat("yyyyMMdd").parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 10位格式化日期
	 */
	public static Date dateParse10(String date) {
		try {
			return new SimpleDateFormat("yyyy/MM/dd").parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static boolean equalsCurrDate(Date date) {
		Calendar tmp = DateUtil.currCalendarDate();
		tmp.setTime(date);
		Calendar currCalendarDate = DateUtil.currCalendarDate();
		return (currCalendarDate.get(Calendar.YEAR) == tmp.get(Calendar.YEAR))
				&& (currCalendarDate.get(Calendar.MONTH) == tmp
						.get(Calendar.MONTH))
				&& (currCalendarDate.get(Calendar.DATE) == tmp
						.get(Calendar.DATE));
	}

	public static Date formatDate(String date, String pattern)
			throws ParseException {
		if (StringUtils.isEmpty(date)) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.parse(date);
	}

	public static Date formatDateyyyyMMdd(String date) throws ParseException {
		return DateUtil.formatDate(date, "yyyyMMdd");
	}

	/** 获取当天最大值 */
	public static Date maxCurrDay(String date) {
		date += " 23:59:59";
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return DateUtil.currDate();
		}
	}

	/**
	 * 格式化日期
	 * 
	 * @return String
	 */
	public static String reverseDate(String date) {
		if (null == date) {
			return "";
		}
		String d = date.trim();
		if (d.length() != 8) {
			return "";
		}
		return d.substring(6, 8) + d.substring(4, 6) + d.substring(0, 4);
	}

	public static String timeFormat6(Date date) {
		if ((null == date) || date.toString().equals(StringUtils.EMPTY)) {
			return StringUtils.leftPad(StringUtils.EMPTY, 8, "0");
		}
		SimpleDateFormat format = new SimpleDateFormat("HHmmss");
		return format.format(date);
	}

	/**
	 * 日格式為 yyyyMMdd HH:mm:ss
	 * @author Allen
	 * @return yyyymmdd HH:mm:ss
	 */
	public static String dateTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		return format.format( new Date());
	}

	/**
	 * 日格式為 yyyyMMdd HH:mm:ss
	 * @author Vic
	 * @return yyyymmdd HH:mm:ss
	 */
	public static String dateTime(Date date) {
		if ((null == date) || date.toString().equals(StringUtils.EMPTY)) {
			return StringUtils.EMPTY;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		return format.format(date);
	}
}
